﻿using System.IO;
using HPPUtil;
using HPPUtil.Helpers;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace HPP.UnitTest
{
    
    
    /// <summary>
    ///这是 INIClassTest 的测试类，旨在
    ///包含所有 INIClassTest 单元测试
    ///</summary>
    [TestClass()]
    public class INIClassTest
    {


        private TestContext testContextInstance;

        private static readonly string winTempDir = System.Environment.GetEnvironmentVariable("TEMP").GetStringWithLastSlash();
        private readonly string fullpath = winTempDir + "config.ini";

        private readonly string TestSectionName = "Config";
        private readonly string TestKey = "Key";
        private readonly string TestValue = "Value";

        /// <summary>
        ///获取或设置测试上下文，上下文提供
        ///有关当前测试运行及其功能的信息。
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region 附加测试属性
        // 
        //编写测试时，还可使用以下属性:
        //
        //使用 ClassInitialize 在运行类中的第一个测试前先运行代码
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //使用 ClassCleanup 在运行完类中的所有测试后再运行代码
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //使用 TestInitialize 在运行每个测试前先运行代码
        [TestInitialize()]
        public void MyTestInitialize()
        {
            if (File.Exists(fullpath))
            {
                File.Delete(fullpath);
            }

            using (StreamWriter writer = File.CreateText(fullpath))
            {
                writer.WriteLine("["+ TestSectionName + "]");
                writer.Write(TestKey);
                writer.Write("=");
                writer.Write(TestValue);
                
            } 
        }
        
        //使用 TestCleanup 在运行完每个测试后运行代码
        [TestCleanup()]
        public void MyTestCleanup()
        {
            if (File.Exists(fullpath))
            {
                File.Delete(fullpath);
            }
        }
        
        #endregion


        /// <summary>
        ///IniReadValue 的测试
        /// 
        /// 输入:不存在的文件路径
        /// 
        /// 输出:null
        ///</summary>
        [TestMethod()]
        public void Test_IniReadValue_when_file_does_not_exists()
        {
            string INIPath = "c:\\123.txt"; 
            INIClass target = new INIClass(INIPath); 
            string Section = string.Empty;
            string Key = string.Empty; 
            string expected = null; 
            string actual;
            actual = target.IniReadValue(Section, Key);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///IniReadValue 的测试
        /// 
        /// 输入:file is null
        /// 
        /// 输出:null
        ///</summary>
        [TestMethod()]
        public void Test_IniReadValue_when_file_is_null()
        {
            string INIPath = null;
            INIClass target = new INIClass(INIPath);
            string Section = string.Empty;
            string Key = string.Empty;
            string expected = null;
            string actual;
            actual = target.IniReadValue(Section, Key);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///IniReadValue 的测试
        /// 
        /// 输入:file is empty
        /// 
        /// 输出:null
        ///</summary>
        [TestMethod()]
        public void Test_IniReadValue_when_file_is_empty()
        {
            string INIPath = string.Empty;
            INIClass target = new INIClass(INIPath);
            string Section = string.Empty;
            string Key = string.Empty;
            string expected = null;
            string actual;
            actual = target.IniReadValue(Section, Key);
            Assert.AreEqual(expected, actual);
        }


        /// <summary>
        ///IniReadValue 的测试
        /// 
        /// 输入:section is empty
        /// 
        /// 输出:null
        ///</summary>
        [TestMethod()]
        public void Test_IniReadValue_when_section_is_empty()
        {
            string INIPath = fullpath;
            INIClass target = new INIClass(INIPath);
            string Section = string.Empty;
            string Key = string.Empty;
            string expected = null;
            string actual;
            actual = target.IniReadValue(Section, Key);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///IniReadValue 的测试
        /// 
        /// 输入:section is null
        /// 
        /// 输出:null
        ///</summary>
        [TestMethod()]
        public void Test_IniReadValue_when_section_is_null()
        {
            string INIPath = fullpath;
            INIClass target = new INIClass(INIPath);
            string Section = null;
            string Key = string.Empty;
            string expected = null;
            string actual;
            actual = target.IniReadValue(Section, Key);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///IniReadValue 的测试
        /// 
        /// 输入:section doesn't exist
        /// 
        /// 输出:null
        ///</summary>
        [TestMethod()]
        public void Test_IniReadValue_when_section_does_not_exist()
        {
            string INIPath = fullpath;
            INIClass target = new INIClass(INIPath);
            string Section = "SFDSFSDFSDFDSFD";
            string Key = string.Empty;
            string expected = null;
            string actual;
            actual = target.IniReadValue(Section, Key);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///IniReadValue 的测试
        /// 
        /// 输入:key is empty
        /// 
        /// 输出:null
        ///</summary>
        [TestMethod()]
        public void Test_IniReadValue_when_key_is_empty()
        {
            string INIPath = fullpath;
            INIClass target = new INIClass(INIPath);
            string Section = TestSectionName;
            string Key = string.Empty;
            string expected = null;
            string actual;
            actual = target.IniReadValue(Section, Key);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///IniReadValue 的测试
        /// 
        /// 输入:key is null
        /// 
        /// 输出:null
        ///</summary>
        [TestMethod()]
        public void Test_IniReadValue_when_key_is_null()
        {
            string INIPath = fullpath;
            INIClass target = new INIClass(INIPath);
            string Section = TestSectionName;
            string Key = null;
            string expected = null;
            string actual;
            actual = target.IniReadValue(Section, Key);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///IniReadValue 的测试
        /// 
        /// 输入:key does not exist
        /// 
        /// 输出:null
        ///</summary>
        [TestMethod()]
        public void Test_IniReadValue_when_key_does_not_exist()
        {
            string INIPath = fullpath;
            INIClass target = new INIClass(INIPath);
            string Section = TestSectionName;
            string Key = "SDFSFSFDFS";
            string expected = null;
            string actual;
            actual = target.IniReadValue(Section, Key);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///IniReadValue 的测试
        /// 
        /// 输入:存在的文件，存在的Section，存在的key
        /// 
        /// 输出:对应的value
        ///</summary>
        [TestMethod()]
        public void Test_IniReadValue_when_file_section_key_exist()
        {
            string INIPath = fullpath;
            INIClass target = new INIClass(INIPath);
            string Section = TestSectionName;
            string Key = TestKey;
            string expected = TestValue;
            string actual;
            actual = target.IniReadValue(Section, Key);
            Assert.AreEqual(expected, actual);
        }


    }
}
